﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ExamSharedClasses;

namespace ExamSharedClasses.Commands {
    /// <summary>
    /// Es enthält ein Exam-Objekt, welches beliebig spezialisiert sein kann.
    /// GetWrittenExamText() kann nur auf ein ExamUser-Objekt angewendet werden.
    /// </summary>
    [Serializable()]
    public class ViewExam : Command {

        public Exam Exam { get; set; }

        public ViewExam()
            : base() {
                this.Exam = new Exam();
        }
        public ViewExam( int examId )
            : base() {
                this.Exam = new Exam(1);
        }
        public ViewExam( Exam exam )
            : base() {
            this.Exam = exam;
        }

        public bool IsExamTimeOver() {
            return ( DateTime.Now >= ((ExamUser)Exam).EndDateTime );
        }

        public string GetWrittenExamText() {
            StringBuilder sb = new StringBuilder();
            if ( Exam is ExamUser ) {

                ExamUser e = (ExamUser)Exam;

                sb.Append( " Klausur Nr.: " + e.ExamId + " Name: " + e.ExamName + " Datum: " + e.ExamWriteDate + '\n' );
                sb.Append( " Klasse: " + e.ExamClass.ClassName + " Modul: " + e.ExamModule.ModuleName + '\n' );
                sb.Append( " Dozent: " + e.ExamAuthor.UserEmail + " (" + e.ExamAuthor.UserId + ") " + '\n' );
                sb.Append( " Anz. d. Fragen: " + e.AnswerList.Count + ", max. Punkte: " + e.GetMaxPoints() + " Dauer (Min.): " + (e.ExamDuration/60) + '\n' );
                sb.Append( '\n' );
                sb.Append( " Teilnehmer: " + e.UserDetails.UserName + " " + e.UserDetails.UserSurName + '\n' );
                sb.Append( " geschrieben: am " + e.StartDateTime + " bis " + e.EndDateTime + '\n' );
                sb.Append( " erreichte Punkte: " + e.GetAchievedPoints() + "\n" );

                sb.Append( '\n' );

                int i = 0;
                foreach ( Answer a in e.AnswerList ) {
                    Question q = a.AnswerQuestion;
                    sb.Append( q.ToString() );
                    sb.Append( a.ToString() );
                    sb.Append( "Punkte: " + a.GetAchievedPoints( e.PointsList[i] ) + " von " + e.PointsList[i] + " möglichen.\n" );
                    sb.Append( '\n' );
                    ++i;
                }

                sb.Append( '\n' );

            } else {
                return "Fehler: Klausur nicht korrekt ausgefüllt";
            }
            return sb.ToString();
        }
 
    
    
    }
}
